package org.vectrics.recruiter.struts.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;


import org.apache.struts2.interceptor.SessionAware;
import org.vectrics.company.Company;
import org.vectrics.company.CompanyService;
import org.vectrics.employee.Employee;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.recruiter.RecruitServiceLocator;
import org.vectrics.user.Person;

import com.hexacta.hrs.services.employee.EmployeeService;
import com.opensymphony.xwork2.ActionSupport;

public class UpdateRecruitersAction extends ActionSupport implements SessionAware{
	
	private static final long serialVersionUID = 1L;
	
	private Company company = null;
	
	private EmployeeService employeeService;
	
	private CompanyService companyService;
	
	private Map<String, Object> session;
	
	private String[] selectedRecruiters;
	
	public String updateRecruiters(){
		List<Employee> recruiters = new ArrayList<Employee>();
		try {
			selectedRecruiters = getSelectedRecruiters();
			for (int i = 0; i < selectedRecruiters.length; i++) {
				Employee employee = employeeService.searchById(Long.valueOf(selectedRecruiters[i]));
				recruiters.add(employee);
			}
		} catch (Exception e) {
		}
		companyService.updateRecruiters(recruiters);
		
		return SUCCESS;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Employee> getRecruitersList() {
		company = CoreServiceLocator.getUserAuthService().getAuthorizedPerson(this.getSession()).getDomain().getPrimaryCompany();
		List<Person> persons= new ArrayList<Person>(RecruitServiceLocator.getRecruitCompanyService().findRecruiters(company));
		List<Employee> recruiters = new ArrayList<Employee>();
		for (Person person : persons) {
			Employee employee = employeeService.searchByPerson(person);
			recruiters.add(employee);
		}
		return recruiters;
	}
	
	 public List<Employee> getAllEmployeesList() {
		 List<Employee> employees = (List<Employee>) employeeService.getAllCurrentEmployees();
		 for (Employee employee : getRecruitersList()) {
			 if (employees.contains(employee)) {
				 employees.remove(employee);
			}
		}
		 Collections.sort(employees,new Comparator<Employee>() {
			@Override
			public int compare(Employee o1, Employee o2) {
				return o1.getPerson().getName().compareToIgnoreCase(o2.getPerson().getName());
			}
		});
		 return employees;
    }
	 
	 public void setSelectedRecruiters(String[] selectedRecruiters) {
		this.selectedRecruiters = selectedRecruiters;
	}

	public String[] getSelectedRecruiters(){
		 return selectedRecruiters;
	 }
	 
	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public EmployeeService getEmployeeService() {
		return employeeService;
	}

	public CompanyService getCompanyService() {
		return companyService;
	}

	public void setCompanyService(CompanyService companyService) {
		this.companyService = companyService;
	}

	public void setEmployeeService(EmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
}
